const socket = require('./socket');
const {
  getObjLength
} = require('../../lib/util')

// 收集的玩家操作发送给所有人
class Frame {
  constructor(f) {
    this.f = f;
    this.frameId = 0;

    this.actions = [];
  }

  init(client) {
    setInterval(() => {
      if (getObjLength(client.players) <= 0) return;
      this.frameId++;
      let data = [];
      data.push(this.frameId);
      Object.keys(client.players).forEach(key => {
        let item = client.players[key];
        data.push([item.operation, key]);
        item.operation = [];
      })
      this.actions.push({
        action: 'operaction',
        data,
        time: new Date().getTime()
      });
      socket.sendAll(this.actions[this.actions.length - 1]);
    }, 1000 / this.f);
  }
}

module.exports = Frame;